package cn.icanci.loopstack.bic.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import cn.icanci.loopstack.bic.biz.service.NodeVueService;
import cn.icanci.loopstack.bic.common.model.TextValue;
import cn.icanci.loopstack.bic.common.model.base.NodeVueVO;
import cn.icanci.loopstack.bic.common.result.R;
import cn.icanci.loopstack.bic.web.form.NodeVueQueryForm;
import cn.icanci.loopstack.bic.web.mapper.NodeVueWebMapper;
import cn.icanci.loopstack.bic.web.model.NodeVue;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/13 22:23
 */
@RestController
@RequestMapping("/bicAdmin/nodeVue")
public class NodeVueController {

    @Resource
    private NodeVueWebMapper nodeVueWebMapper;
    @Resource
    private NodeVueService   nodeVueService;

    // ================================ CRUD ================================
    @PostMapping("query")
    public R query(@RequestBody NodeVueQueryForm form) {
        return R.builderOk()
            .data("queryPage", nodeVueService.queryPage(nodeVueWebMapper.web2vo(form.getNodeVue()), form.getPaginator().getCurrentPage(), form.getPaginator().getPageSize()))
            .build();
    }

    @PostMapping("save")
    public R save(@RequestBody NodeVue nodeVue) {
        nodeVueService.save(nodeVueWebMapper.web2vo(nodeVue));
        return R.builderOk().build();
    }

    @GetMapping("validateName/{systemUuid}/{name}")
    public R validateName(@PathVariable("systemUuid") String systemUuid, @PathVariable("name") String name) {
        NodeVueVO nodeVue = nodeVueService.queryBySystemUuidAndName(systemUuid, name);
        return R.builderOk().data("result", nodeVue == null).build();
    }

    @GetMapping("validateKey/{systemUuid}/{key}")
    public R validateKey(@PathVariable("systemUuid") String systemUuid, @PathVariable("key") String key) {
        NodeVueVO nodeVue = nodeVueService.queryBySystemUuidAndKey(systemUuid, key);
        return R.builderOk().data("result", nodeVue == null).build();
    }
    // ================================ Load Selectors ================================

    @GetMapping("loadSelector/{systemUuid}/{nodeUuid}")
    public R loadSelector(@PathVariable("systemUuid") String systemUuid, @PathVariable("nodeUuid") String nodeUuid) {
        List<TextValue> textValues = nodeVueService.loadSelector(systemUuid, nodeUuid);
        return R.builderOk().data("textValues", textValues).build();
    }

    @GetMapping("loadDefaultVue")
    public R loadDefaultVue() {
        String defaultStyle = nodeVueService.loadDefaultVue();
        return R.builderOk().data("nodeVueCode", defaultStyle).build();
    }
}
